package com.supermarket.model;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;

/**
 * 销售订单实体类
 * 对应数据库中的sales_orders表
 */
public class SalesOrder {
    private int id;
    private String orderNo;
    private int cashierId;
    private String cashierName; // 收银员姓名（用于显示）
    private BigDecimal totalAmount;
    private BigDecimal discountAmount;
    private BigDecimal finalAmount;
    private String paymentMethod;
    private String orderStatus;
    private Date orderDate;
    private Time orderTime;
    private Timestamp createdAt;

    // 订单明细列表
    private List<SalesDetail> salesDetails;
    private int itemCount; // 商品种类数量

    // 构造函数
    public SalesOrder() {
        this.totalAmount = BigDecimal.ZERO;
        this.discountAmount = BigDecimal.ZERO;
        this.finalAmount = BigDecimal.ZERO;
    }

    public SalesOrder(String orderNo, int cashierId, String paymentMethod) {
        this();
        this.orderNo = orderNo;
        this.cashierId = cashierId;
        this.paymentMethod = paymentMethod;
        this.orderStatus = "PENDING";
        this.orderDate = new Date(System.currentTimeMillis());
        this.orderTime = new Time(System.currentTimeMillis());
    }

    // Getter和Setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public int getCashierId() {
        return cashierId;
    }

    public void setCashierId(int cashierId) {
        this.cashierId = cashierId;
    }

    public String getCashierName() {
        return cashierName;
    }

    public void setCashierName(String cashierName) {
        this.cashierName = cashierName;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getDiscountAmount() {
        return discountAmount;
    }

    public void setDiscountAmount(BigDecimal discountAmount) {
        this.discountAmount = discountAmount;
    }

    public BigDecimal getFinalAmount() {
        return finalAmount;
    }

    public void setFinalAmount(BigDecimal finalAmount) {
        this.finalAmount = finalAmount;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public Time getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Time orderTime) {
        this.orderTime = orderTime;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    public List<SalesDetail> getSalesDetails() {
        return salesDetails;
    }

    public void setSalesDetails(List<SalesDetail> salesDetails) {
        this.salesDetails = salesDetails;
        this.itemCount = salesDetails != null ? salesDetails.size() : 0;
    }

    public int getItemCount() {
        return itemCount;
    }

    public void setItemCount(int itemCount) {
        this.itemCount = itemCount;
    }

    /**
     * 获取支付方式中文名称
     */
    public String getPaymentMethodName() {
        switch (paymentMethod) {
            case "CASH":
                return "现金";
            case "CARD":
                return "刷卡";
            case "MOBILE":
                return "移动支付";
            default:
                return paymentMethod;
        }
    }

    /**
     * 获取订单状态中文名称
     */
    public String getOrderStatusName() {
        switch (orderStatus) {
            case "PENDING":
                return "待处理";
            case "COMPLETED":
                return "已完成";
            case "CANCELLED":
                return "已取消";
            default:
                return orderStatus;
        }
    }

    /**
     * 计算总金额（基于明细）
     */
    public void calculateTotalAmount() {
        if (salesDetails != null) {
            totalAmount = salesDetails.stream()
                    .map(SalesDetail::getSubtotal)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            finalAmount = totalAmount.subtract(discountAmount);
        }
    }

    /**
     * 添加销售明细
     */
    public void addSalesDetail(SalesDetail detail) {
        if (salesDetails != null) {
            salesDetails.add(detail);
            calculateTotalAmount();
        }
    }

    @Override
    public String toString() {
        return "SalesOrder{" +
                "id=" + id +
                ", orderNo='" + orderNo + '\'' +
                ", cashierName='" + cashierName + '\'' +
                ", totalAmount=" + totalAmount +
                ", discountAmount=" + discountAmount +
                ", finalAmount=" + finalAmount +
                ", paymentMethod='" + paymentMethod + '\'' +
                ", orderStatus='" + orderStatus + '\'' +
                ", orderDate=" + orderDate +
                ", orderTime=" + orderTime +
                ", itemCount=" + itemCount +
                '}';
    }
}
